package cn.team11.sds.dayi.debugpage;

/**
 * Created by Mr.liu on 2018/1/10.
 */

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import cn.team11.sds.dayi.R;
import cn.team11.util.ImageUtils;

/**
 * Created by Administrator on 2017/2/28.
 * 邮箱：wgyscsf@163.com
 * 博客：http://blog.csdn.net/wgyscsf
 */

public class CustomCaptureActivity extends AppCompatActivity {

    TextView btnPhoto;
//    TextView btnBack;

//    qrcode_btn_back
    public static final int REQUEST_CODE=1;
    public static final int REQUEST_IMAGE=2;
    public static final int REQUEST_CUSTOM=3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customcapture);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        btnPhoto= (TextView) findViewById(R.id.qrcode_btn_photo);
//        btnBack= ((TextView) findViewById(R.id.qrcode_btn_back));
        Drawable drawable1=getResources().getDrawable(R.drawable.selector_ic_photo);

        Rect r = new Rect(0, 0, drawable1.getMinimumWidth() * 2 / 8, drawable1.getMinimumHeight() * 2 / 8);
        //定义一个Rect边界
        drawable1.setBounds(r);
        //给每一个RadioButton设置图片大小
        btnPhoto.setCompoundDrawables(null, drawable1, null, null);

        CaptureFragment captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
        /**
         * 二维码解析回调函数
         */
        CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                Intent resultIntent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                bundle.putString(CodeUtils.RESULT_STRING, result);
                resultIntent.putExtras(bundle);
                setResult(RESULT_OK, resultIntent);
                finish();
            }

            @Override
            public void onAnalyzeFailed() {
                Intent resultIntent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
                bundle.putString(CodeUtils.RESULT_STRING, "");
                resultIntent.putExtras(bundle);
                setResult(RESULT_OK, resultIntent);
                finish();
            }
        };
        captureFragment.setAnalyzeCallback(analyzeCallback);
        /**
         * 替换我们的扫描控件
         */
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();
        btnPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                // 开启一个带有返回值的Activity，请求码为PHOTO_REQUEST_GALLERY
                startActivityForResult(intent, REQUEST_IMAGE);
            }
        });

//        btnBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);

//      QRCodeHelper.onActivityResult(this, requestCode, resultCode, data);
        if(requestCode==REQUEST_CODE){
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if(bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Intent resultIntent = new Intent();
//                    Bundle bundle = new Bundle();
                    bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                    bundle.putString(CodeUtils.RESULT_STRING, result);
                    resultIntent.putExtras(bundle);
                    CustomCaptureActivity.this.setResult(RESULT_OK, resultIntent);
                    CustomCaptureActivity.this.finish();
//                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();


                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Intent resultIntent = new Intent();
                    bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
                    bundle.putString(CodeUtils.RESULT_STRING, "");
                    resultIntent.putExtras(bundle);
                    CustomCaptureActivity.this.setResult(RESULT_OK, resultIntent);
                    CustomCaptureActivity.this.finish();

                    //Toast.makeText(this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
        if (requestCode == REQUEST_IMAGE) {
            if (data != null) {
                Uri uri = data.getData();
                ContentResolver cr = getContentResolver();
                try {
                    Bitmap mBitmap = MediaStore.Images.Media.getBitmap(cr, uri);//显得到bitmap图片
                    CodeUtils.analyzeBitmap(ImageUtils.getImageAbsolutePath(this, uri), new CodeUtils.AnalyzeCallback() {
                        @Override
                        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                            Intent resultIntent = new Intent();
                            Bundle bundle = new Bundle();
                            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                            bundle.putString(CodeUtils.RESULT_STRING, result);
                            resultIntent.putExtras(bundle);
                            CustomCaptureActivity.this.setResult(RESULT_OK, resultIntent);
                            CustomCaptureActivity.this.finish();
                            //Toast.makeText(CustomCaptureActivity.this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onAnalyzeFailed() {
                            Intent resultIntent = new Intent();
                            Bundle bundle = new Bundle();
                            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
                            bundle.putString(CodeUtils.RESULT_STRING, "");
                            resultIntent.putExtras(bundle);
                            CustomCaptureActivity.this.setResult(RESULT_OK, resultIntent);
                            CustomCaptureActivity.this.finish();
//                            Toast.makeText(CustomCaptureActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                        }
                    });

                    if (mBitmap != null) {
                        mBitmap.recycle();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if(requestCode==REQUEST_CUSTOM){
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Intent resultIntent = new Intent();
                    bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                    bundle.putString(CodeUtils.RESULT_STRING, result);
                    resultIntent.putExtras(bundle);
                    CustomCaptureActivity.this.setResult(RESULT_OK, resultIntent);
                    CustomCaptureActivity.this.finish();

//                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Intent resultIntent = new Intent();
                    bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
                    bundle.putString(CodeUtils.RESULT_STRING, "");
                    resultIntent.putExtras(bundle);
                    CustomCaptureActivity.this.setResult(RESULT_OK, resultIntent);
                    CustomCaptureActivity.this.finish();
//                    Toast.makeText(this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }

        }

    }
}
